# coding=utf-8
import time
import os,sys
BASE_PATH = os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))
)
sys.path.insert(0,BASE_PATH)

from bin import get_Webdriver
from bin.getCheckCode import GetCheckCode
from conf import settings

class Login_Action:
    def __init__(self):
        self.driver = get_Webdriver.Get_Webdriver().driver

    def jumnptoLoginpage(self):
        self.driver.open(settings.loginUrl)

    def login(self, telphoneOrMail="", checkCode="", clickLogingButton="点击", token=None):
        self.jumnptoLoginpage()
        curl = self.driver.get_url()
        print(curl)
        if '/login.html' in curl:
            self.inputTelphoneOrMail(telphoneOrMail)
            if checkCode == "不获取":
                self.clickGetCheckCode()
            elif checkCode == "获取":
                self.clickGetCheckCode()
                if telphoneOrMail != "":
                    time.sleep(2.5)
                    checkCode = GetCheckCode().getCheckCode(telphoneOrMail, token)
                    self.inputCheckCode(checkCode)
                    print("token---{}".format(token))
                    print("checkCodeid---{}".format(checkCode))
            else:
                self.inputCheckCode(checkCode)
            if clickLogingButton == "点击":
                self.clickLogingButton()
        elif '/searchV2/main.html' in curl:
            pass
        else:
            raise UserWarning('登录页打开失败')

    def inputTelphoneOrMail(self, telphoneOrMail):
        """输入手机号或邮箱"""
        self.driver.clear("css=>#phone1")
        self.driver.type("css=>#phone1", telphoneOrMail)

    def clickGetCheckCode(self):
        """点击获取验证码"""
        self.driver.click("css=>#sendMsg")

    def inputCheckCode(self, checkCode):
        """输入验证码"""
        self.driver.clear("css=>#code1")
        self.driver.type("css=>#code1", checkCode)

    def clickLogingButton(self):
        """点击登录按钮"""
        self.driver.click("css=>#msglogin")

    def isNousePage(self):
        # urlCheck = ("error/defind.html" in self.driver.get_url)
        urlCheck = self.driver.get_url()
        # textCheck = ("您没有权限访问当前系统" in self.dr.find_element_by_xpath("html/body/section/div/section").text)
        textCheck = self.driver.get_text("css=>.error-wrapper.text-center>h3")
        return urlCheck, textCheck

    def refresh(self):
        """刷新"""
        self.driver.F5()

    def quit(self):
        self.driver.quit()

    def close(self):
        self.driver.close()


if __name__ == '__main__':
    Login_Action().jumnptoLoginpage()
    Login_Action().login(telphoneOrMail="13581982750",checkCode="000000",clickLogingButton="点击")
